from operator import truediv
import time
from PySide2.QtCore import QTime

class CatchTime:
    isoff = False

    # days是指定间隔多少天
    def __init__(self, timeInfo):
        self.timeInfo = timeInfo
        self.timeStamp = {}
        for i in timeInfo:
            self.timeStamp[i] = self.getTimeStamp(self.timeInfo[i])
        self.timeStamp['tpEndPoint'] = self.getTimeStamp(QTime(23,59,59))

    #获取当日时间点的时间戳
    def getTimeStamp(self, qtime):
        now = time.localtime()
        return time.mktime((now.tm_year,now.tm_mon,now.tm_mday,qtime.hour(),qtime.minute(),qtime.second(),now.tm_wday,now.tm_yday,0))

    #当前时间是否在抓取时间内
    def isIn(self):
        ts_now = time.time()
        if self.timeStamp['tpStart1']<=ts_now<=self.timeStamp['tpEnd1'] or self.timeStamp['tpStart2']<=ts_now<=self.timeStamp['tpEnd2']:
            return True
        else:
            return False

    #当前时间是上午时间段还是下午时间段
    def sorx(self):
        ts_now = time.time()
        if self.timeStamp['tpStart1']<=ts_now<=self.timeStamp['tpEnd1']:
            return 1
        elif self.timeStamp['tpStart2']<=ts_now<=self.timeStamp['tpEnd2']:
            return 2
        else:
            return 0

    def isOff(self):
        #今天的时间已经结束
        ts_now = time.time()
        if ts_now > self.timeStamp['tpEnd2']:
            return True
        else:
            return False
